﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Data.SystemManager
{
    public class TaskRemindRepository : RepositoryBase<TaskRemind>
    {
        private readonly UserRepository _userRepository = new UserRepository();
        private readonly ModuleRepository _service = new ModuleRepository();
        private readonly RelevanceRepository _relevanceRepository = new RelevanceRepository();
        private readonly InvoiceRepository _invoiceRepository = new InvoiceRepository();
        private readonly FinanceInvoiceRepository _financeInvoiceRepository = new FinanceInvoiceRepository();
        private readonly TechnologyRepository _technologyRepository = new TechnologyRepository();
        private readonly PurchaseRepository _purchaseRepository = new PurchaseRepository();
        private readonly WorksheetRepository _worksheetRepository = new WorksheetRepository();
        private readonly OriginalCostRepository _originalCostRepository = new OriginalCostRepository();
        private readonly FinanceCostRepository _financeCostRepository = new FinanceCostRepository();
        private readonly OrderRepository _orderRepository = new OrderRepository();
        private readonly ExamineRepository _examineRepository = new ExamineRepository();
        private readonly StorageTableRepository _storageTableRepository = new StorageTableRepository();
        private readonly ProductRepository _productRepository = new ProductRepository();

        /// <summary>
        /// 更新提醒数据
        /// </summary>
        /// <returns></returns>
        public bool UpdateAll()
        {
            DateTime start = Convert.ToDateTime("2017-1-1");
            DateTime end = DateTime.Now.AddDays(1);
            int count = 0;
            TaskRemind tr = new TaskRemind();

            //工艺核算
            //count = dbcontext.Technologies.Count(a => a.Status == -1);
            count = _technologyRepository.GetList(-1, "", "", "", start, end).Count();
            tr = dbcontext.TaskReminds.First(a => a.Number == 1);
            tr.Count = count;
            dbcontext.SaveChanges();

            //成本核算
            //count = dbcontext.OriginalCosts.Count(a => a.Status == -1);
            count = _originalCostRepository.GetList(-1, "", "", "").Count();
            tr = dbcontext.TaskReminds.First(a => a.Number == 2);
            tr.Count = count;
            dbcontext.SaveChanges();

            //价格核算
            //count = dbcontext.FinanceCosts.Count(a => a.Status == -1);
            count = _financeCostRepository.GetList(4, -1, "", "", start, end).Count();
            tr = dbcontext.TaskReminds.First(a => a.Number == 3);
            tr.Count = count;
            dbcontext.SaveChanges();

            //确认指导价
            //count = dbcontext.Orders.Count(a => a.Status == 5 && a.Stage == 2);
            count = _orderRepository.GetList(start, end, 2, 5, "", "", "").Count();
            tr = dbcontext.TaskReminds.First(a => a.Number == 4);
            tr.Count = count;
            dbcontext.SaveChanges();

            //提交最终价【特殊处理：前台根据登陆用户计算】
            //count = dbcontext.Orders.Count(a => a.Status == 501 && a.Stage == 2);
            //tr = dbcontext.TaskReminds.First(a => a.Number == 5);
            //tr.Count = count;
            //dbcontext.SaveChanges();

            //审核最终价
            //count = dbcontext.Orders.Count(a => a.Status == 502 && a.Stage == 2);
            count = _orderRepository.GetList(start, end, 2, 502, "", "", "").Count();
            tr = dbcontext.TaskReminds.First(a => a.Number == 6);
            tr.Count = count;
            dbcontext.SaveChanges();

            //确认下单【特殊处理：前台根据登陆用户计算】
            //count = dbcontext.Orders.Count(a => a.Status == 6 && a.Stage == 2);
            //tr = dbcontext.TaskReminds.First(a => a.Number == 7);
            //tr.Count = count;
            //dbcontext.SaveChanges();

            //订单分配
            //count = dbcontext.Purchases.Count(a => a.Status == 1);
            count = _purchaseRepository.GetList(1, -10, "", "", start, end).Count();
            tr = dbcontext.TaskReminds.First(a => a.Number == 8);
            tr.Count = count;
            dbcontext.SaveChanges();

            //获取所有采购单
            List<ProductTechnologyView> pLs = _purchaseRepository.GetPurList(1, -10, "", "", start, end).ToList();
            //填采购单
            //count = dbcontext.PurchaseComposes.Count(a => a.Status == 1);
            count = pLs.FindAll(a => a.PurchaseStatus == 1).Count();
            tr = dbcontext.TaskReminds.First(a => a.Number == 9);
            tr.Count = count;
            dbcontext.SaveChanges();
            //确认采购
            //count = dbcontext.PurchaseComposes.Count(a => a.Status == 3);
            count = pLs.FindAll(a => a.PurchaseStatus == 3).Count();
            tr = dbcontext.TaskReminds.First(a => a.Number == 10);
            tr.Count = count;
            dbcontext.SaveChanges();

            //加工
            //count = dbcontext.Worksheets.Count(a => a.Status == -1);
            count = _worksheetRepository.GetList(-1, "", "", start, end).Count(); ;
            tr = dbcontext.TaskReminds.First(a => a.Number == 11);
            tr.Count = count;
            dbcontext.SaveChanges();

            //完善加工单
            //count = dbcontext.Worksheets.Count(a => a.Status == 2);
            List<ProductTechnologyView> wLs = _worksheetRepository.GetList(4, "", "", start, end).ToList();
            count = wLs.FindAll(a => a.Status == 2).Count();
            tr = dbcontext.TaskReminds.First(a => a.Number == 12);
            tr.Count = count;
            dbcontext.SaveChanges();

            //检验
            //count = dbcontext.Examines.Count(a => a.Status == -1);
            count = _examineRepository.GetList(-1, "", start, end).Count();
            tr = dbcontext.TaskReminds.First(a => a.Number == 13);
            tr.Count = count;
            dbcontext.SaveChanges();

            //入库
            //count = dbcontext.StorageTables.Count(a => a.Status == -1 && a.Type == 2);
            count = _storageTableRepository.GetList(2, start, end, "").Count();
            tr = dbcontext.TaskReminds.First(a => a.Number == 14);
            tr.Count = count;
            dbcontext.SaveChanges();

            //出库
            //count = dbcontext.StorageTables.Count(a => a.Status == -1 && a.Type == 1);
            count = _storageTableRepository.GetList(1,  start, end, "").Count();
            tr = dbcontext.TaskReminds.First(a => a.Number == 15);
            tr.Count = count;
            dbcontext.SaveChanges();

            //发货 
            count = dbcontext.Invoices.Count(a => a.Status == 1);
            tr = dbcontext.TaskReminds.First(a => a.Number == 18);
            tr.Count = count;
            dbcontext.SaveChanges();

            //对账
            count = _invoiceRepository.GetInvoiceListGroupByOrder("").Count();
            tr = dbcontext.TaskReminds.First(a => a.Number == 19);
            tr.Count = count;
            dbcontext.SaveChanges();

            //开票 
            count = _financeInvoiceRepository.GetFinanceInvoicesList(-1, "").Count();
            tr = dbcontext.TaskReminds.First(a => a.Number == 20);
            tr.Count = count;
            dbcontext.SaveChanges();

            //采购审核
            count = _purchaseRepository.GetPurList(2, -10, "", "", start, end).Count();
            tr = dbcontext.TaskReminds.First(a => a.Number == 23);
            tr.Count = count;
            dbcontext.SaveChanges();

            //产品审核
            count = _productRepository.IQueryable(a => a.Status == -1).Count();
            tr = dbcontext.TaskReminds.First(a => a.Number == 24);
            tr.Count = count;
            dbcontext.SaveChanges();

            //运输审核
            count = _invoiceRepository.IQueryable(a => a.Status == 101).Count();
            tr = dbcontext.TaskReminds.First(a => a.Number == 25);
            tr.Count = count;
            dbcontext.SaveChanges();

            return true;
        }

        /// <summary>
        /// 获取全部提醒列表
        /// </summary>
        /// <returns></returns>
        public IQueryable<TaskRemindView> GetRemindList()
        {
            var result = from remind in dbcontext.TaskReminds
                         join pageTask in dbcontext.PageTasks
                         on remind.Number equals pageTask.TaskID
                         join module in dbcontext.Modules
                         on pageTask.PageID equals module.Id
                         where remind.Count > 0
                         select new TaskRemindView
                         {
                             Id = remind.Id,
                             Number = remind.Number,
                             Name = remind.Name,
                             Sort = remind.Sort,
                             Count = remind.Count,
                             Url = module.Url
                         };
            return result;
        }

        /// <summary>
        /// 获取用户能打开的页面
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public List<Module> GetUserPageList(Guid userId)
        {
            //管理员全部页面
            User user = _userRepository.FindEntity(a => a.Id == userId);
            if (user.UserName == "admin")
            {
                return _service.IQueryable().ToList();
            }

            //用户角色
            var userRoleIds =
                _relevanceRepository.IQueryable(u => u.FirstId == userId && u.Type == "UserRole").Select(u => u.SecondId).ToList();

            //用户角色与自己分配到的模块ID
            var moduleIds = _relevanceRepository.IQueryable(u => (u.FirstId == userId && u.Type == "UserModule") || (u.Type == "RoleModule" && userRoleIds.Contains(u.FirstId))).Select(u => u.SecondId).ToList();

            if (!moduleIds.Any()) return new List<Module>();
            return _service.IQueryable(u => moduleIds.Contains(u.Id)).OrderBy(a => a.SortNo).ToList();
        }

        /// <summary>
        /// 获取某用户的提醒列表
        /// </summary>
        /// <returns></returns>
        public List<TaskRemindView> GetRemindListByUser(Guid userId)
        {
            List<TaskRemindView> ls = new List<TaskRemindView>();

            List<TaskRemindView> lsAll = GetRemindList().ToList();
            List<Module> lsModule = GetUserPageList(userId);
            List<string> lsUrl = new List<string>();
            foreach (Module m in lsModule)
            {
                lsUrl.Add(m.Url);
            }

            foreach (TaskRemindView item in lsAll)
            {
                if (lsUrl.Contains(item.Url))
                {
                    ls.Add(item);
                }
            }

            return ls;
        }

    }
}
